from typing import Any, TypeVar

from sqlalchemy import select, Select, Delete
from sqlalchemy.sql.dml import ReturningInsert, ReturningUpdate

from recruit.adapter.database.gateway.base import SQLAlchemyGateway
from recruit.adapter.database.models import Base

ResumeModel = TypeVar('ResumeModel', bound=Base)


# TODO: Cover tests
class BaseResumeGateway(SQLAlchemyGateway):
    async def create(self, stmt: ReturningInsert[Any]) -> ResumeModel:
        result = await self.session.execute(stmt)
        return result.scalar_one()

    async def get_one(self, stmt: Select[Any]) -> ResumeModel | None:
        result = await self.session.execute(stmt)
        return result.scalar_one_or_none()

    async def update(self, stmt: ReturningUpdate[Any]) -> ResumeModel:
        result = await self.session.execute(stmt)
        return result.scalar_one()

    async def check_resume(self, stmt: Select[Any]) -> bool:
        return await self.session.scalar(select(stmt))

    async def delete(self, stmt: Delete[Any]) -> None:
        await self.session.execute(stmt)
